package com.fjwt.gz.proxy.ctrl.lite.official;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.OfficialMaterialEntity;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.OfficialMediaService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公众号素材管理
 */
@RestController
@Slf4j
@RequestMapping("/api/officialMedia")
public class OfficialMediaController extends CommonCtrl {

    @Autowired
    private OfficialMediaService officialMediaService;

    /**
     * 上传素材接口
     */
    @PreAuthorize("hasAnyAuthority('ENT_OFFICIAL_ACCOUNT_MATERIAL_ADD')")
    @PostMapping("/uploadMedia")
    public ApiRes uploadMedia(MultipartFile file){
        String appid = getValString("appid");
        String type  = getValString("type");
        String state = getValString("state");
        log.info("机构端上传素材接口，请求地址：/api/officialMedia/uploadMedia，请求参数：appid：{}，type：{}，state：{},file：{}", appid, type, state, file);
        JSONObject jsonObject = this.officialMediaService.uploadMedia(appid, type, state, file);
        log.info("机构端上传素材接口，请求地址：/api/officialMedia/uploadMedia，返回结果：{}", JSONUtil.toJsonStr(jsonObject));
        return ApiRes.ok(jsonObject);
    }

    /**
     * 公众号素材图片列表
     */
    @PostMapping("/getMaterialImgList")
    public ApiRes getMaterialImgList(){
        String officialAppId = getValString("officialAppId");
        String activityTemplateNo = getValString("activityTemplateNo");
        log.info("pc端获取公众号素材图片列表接口，请求地址：/api/officialMedia/getMediaImgList，请求参数：activityTemplateNo：{},officialAppId：{}", activityTemplateNo, officialAppId);
        IPage<OfficialMaterialEntity> materialList = officialMediaService.getMaterialImgList(getIPage(), activityTemplateNo, officialAppId);
        log.info("pc端获取公众号素材图片列表接口，请求地址：/api/officialMedia/getNewsMaterialList，返回结果：{}", JSONUtil.toJsonStr(materialList));
        return ApiRes.ok(materialList);
    }

    /**
     * 获取素材列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_OFFICIAL_ACCOUNT_MATERIAL')")
    @PostMapping("/getMaterialList")
    public ApiRes getMaterialList() {
        String appid = getValString("appid");
        String type  = getValString("type");
        Integer begin = getValInteger("begin");
        Integer end = getValInteger("end");
        log.info("机构端获取素材列表接口，请求地址：/api/officialMedia/getMaterialList，请求参数：appid：{}，type：{}，begin：{}，end：{}", appid, type, begin, end);
        WxMpMaterialFileBatchGetResult materialList = officialMediaService.getMaterialList(appid, type, begin, end);
        log.info("机构端获取素材列表接口，请求地址：/api/officialMedia/getMaterialList，返回结果：{}", JSONUtil.toJsonStr(materialList));
        return ApiRes.ok(materialList);
    }

    /**
     * 获取图文素材列表
     */
    @PostMapping("/getNewsMaterialList")
    public ApiRes getNewsMaterialList() {
        String appid = getValString("appid");
        Integer offset = getValInteger("offset");
        Integer count = getValInteger("count");
        log.info("机构端获取图文素材列表接口，请求地址：/api/officialMedia/getNewsMaterialList，请求参数：appid：{}，begin：{}，end：{}", appid, offset, count);
        WxMpMaterialNewsBatchGetResult materialList = officialMediaService.getNewsMaterialList(appid, offset, count);
        log.info("机构端获取图文素材列表接口，请求地址：/api/officialMedia/getNewsMaterialList，返回结果：{}", JSONUtil.toJsonStr(materialList));
        return ApiRes.ok(materialList);
    }
    /**
     * 删除素材
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_OFFICIAL_ACCOUNT_MATERIAL_DEL')")
    @PostMapping("/deleteMaterial")
    public ApiRes deleteMaterial(){
        String appid = getValString("appid");
        String mediaId = getValString("mediaId");
        log.info("机构端删除素材接口，请求地址：/api/officialMedia/deleteMaterial，请求参数：appid：{}，mediaId：{}", appid, mediaId);
        boolean flag = this.officialMediaService.deleteMaterial(appid, mediaId);

        return flag?ApiRes.ok():ApiRes.customFail("操作失败！");
    }

}
